using System;
using System.Collections.Generic;
using System.Text;
using MediaPortal.GUI.Library;
using MediaPortal.Dialogs;
using System.IO;


namespace MediaPortal.GUI.iPlayer
{
    public class mainbit : GUIWindow
    {
        #region SkinControlAttributes
        [SkinControlAttribute(2)]
        protected GUIButtonControl buttonOne = null;
        [SkinControlAttribute(3)]
        protected GUIButtonControl buttonTwo = null;
        Preferences prefs = new Preferences();
        #endregion


         // With GetID it will be an window-plugin / otherwise a process-plugin
        // Enter the id number here again
        public override int GetID
        {
            get
            {
                return 5678;
            }

            set
            {
            }
        }

        public override bool Init()
        {
            
            return Load(GUIGraphicsContext.Skin + @"\iPlay_main.xml");
        }

        protected override void OnClicked(int controlId, GUIControl control,
          MediaPortal.GUI.Library.Action.ActionType actionType)
        {
            if (control == buttonOne)
                OnButtonOne();
            if (control == buttonTwo)
                GUIWindowManager.ActivateWindow(5680);
            base.OnClicked(controlId, control, actionType);
        }

        private void OnButtonOne()
        {
         //   iPlayerService serv = new iPlayerService();
            OnSpick();
            
        }

        private void OnSpick()
        {
            try
            {
                prefs.loadPreferences("");

                if (prefs.logFile != "")
                {

                    GUIDialogOK dlg2 = (GUIDialogOK)GUIWindowManager.GetWindow(
                        (int)GUIWindow.Window.WINDOW_DIALOG_OK);
                    dlg2.SetHeading("Hello");
                    dlg2.SetLine(1, "Updating Program List");
                    dlg2.SetLine(2, String.Empty);
                    dlg2.SetLine(3, String.Empty);
                    dlg2.DoModal(GUIWindowManager.ActiveWindow);

                    if (File.Exists(prefs.logFile))
                    {
                        FileInfo fInfo = new FileInfo(prefs.logFile);
                        if (fInfo.Length > 1000000)
                        {
                            File.Delete(prefs.logFile);
                        }
                    }

                }

                LogToFile("Running Cache Service");
                TVProgramList myList = new TVProgramList(prefs.cacheLocation);
                myList.LoadFromFile();

                if (prefs.getBBC == "1")
                {
                    try
                    {
                        LogToFile("Loading BBC");
                        iPlayerTranslator p1 = new iPlayerTranslator(prefs);
                        myList = p1.loadDatabase(myList, "0");
                        LogToFile("added " + p1.added + ", removed " + p1.removed + ", total " + p1.total);
                    }
                    catch (Exception e1)
                    {
                        LogToFile("Loading Failed with error" + e1.ToString());
                    }

                }
                //if (prefs.getITV == "1")
                //{
                //    try
                //    {
                //        LogToFile("Loading ITV");
                //        ITVTranslator itv = new ITVTranslator(prefs);
                //        myList = itv.loadDatabase(myList, "1");
                //        LogToFile( "added " + itv.added + ", removed " + itv.removed + ", total " + itv.total);
                //    }
                //    catch (Exception e1)
                //    {
                //        LogToFile("Loading Failed with error" + e1.ToString());
                //    }

                //}
                //if (prefs.getC4 == "1")
                //{
                //    try
                //    {
                //        LogToFile("Loading C4");

                //        C4Translator c4 = new C4Translator(prefs);
                //        myList = c4.loadDatabase(myList, "1");
                //        LogToFile("added " + c4.added + ", removed " + c4.removed + ", total " + c4.total);
                //    }
                //    catch (Exception e1)
                //    {
                //        LogToFile("Loading Failed with error" + e1.ToString());
                //    }
                //}
                //if (prefs.getFive == "1")
                //{
                //    try
                //    {
                //        LogToFile("Loading Five");

                //        FiveTranslator Five = new FiveTranslator(prefs);
                //        myList = Five.loadDatabase(myList, "1");
                //        LogToFile("added " + Five.added + ", removed " + Five.removed + ", total " + Five.total);
                //    }
                //    catch (Exception e1)
                //    {
                //        LogToFile("Loading Failed with error" + e1.ToString());
                //    }
                //}
                LogToFile("Writing to " + prefs.cacheLocation);

                myList.WriteToFile();

                prefs.getLatestResolutions();

            }
            catch (Exception e2)
            {
                LogToFile(e2.ToString());
            }
        }

        private void LogToFile(string contents)
        {
             if (prefs.logFile != "")
             {
                //set up a filestream
                FileStream fs = new FileStream(prefs.logFile, FileMode.OpenOrCreate, FileAccess.Write);

                //set up a streamwriter for adding text

                StreamWriter sw = new StreamWriter(fs);

                //find the end of the underlying filestream

                sw.BaseStream.Seek(0, SeekOrigin.End);

                //add the text
                sw.WriteLine(contents);
                //add the text to the underlying filestream

                sw.Flush();
                //close the writer
                sw.Close();
            }
        }

        private void OnButtonTwo()
        {
            GUIDialogOK dlg = (GUIDialogOK)GUIWindowManager.GetWindow(
              (int)GUIWindow.Window.WINDOW_DIALOG_OK);
            dlg.SetHeading("Button has been pressed");
            dlg.SetLine(1, "You pressed button 2");
            dlg.SetLine(2, String.Empty);
            dlg.SetLine(3, String.Empty);
            dlg.DoModal(GUIWindowManager.ActiveWindow);
        }

    }
}
